package com.example.secondhandspringboot.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.example.secondhandspringboot.domain.entity.RequiredGoods;
import com.example.secondhandspringboot.domain.entity.User;
import com.example.secondhandspringboot.domain.vo.RequiredGoodsVO;
import com.example.secondhandspringboot.mapper.RequiredGoodsMapper;
import com.example.secondhandspringboot.results.PageResult;
import com.example.secondhandspringboot.service.IRequiredGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周在林
 * @since 2024-04-25
 */
@Service
@RequiredArgsConstructor
public class RequiredGoodsServiceImpl extends ServiceImpl<RequiredGoodsMapper, RequiredGoods> implements IRequiredGoodsService {

    private final RequiredGoodsMapper requiredGoodsMapper;

    @Override
    public PageResult<RequiredGoodsVO> getRequiredGoods(int page, int pageSize, Long currentUserId) {
        //跟据userId获取schoolId
        User user = Db.lambdaQuery(User.class)
                .eq(User::getId, currentUserId)
                .one();
        Long schoolId = user == null ? null : user.getSchoolId();

        //创建分页对象
        Page<RequiredGoodsVO> page1 = new Page<>(page,pageSize);

        Page<RequiredGoodsVO> pageResult = requiredGoodsMapper.getRequiredGoods(schoolId,page1);

        PageResult<RequiredGoodsVO> result = new PageResult<>();
        result.setTotal(pageResult.getTotal());
        result.setRecords(pageResult.getRecords());
        return result;
    }

    @Override
    public PageResult<RequiredGoodsVO> getMyPushRequiredGoods(int page, int pageSize, Long currentId) {
        Page<RequiredGoodsVO> page1 = new Page<>(page,pageSize);
        Page<RequiredGoodsVO> pageResult = requiredGoodsMapper.getMyPushRequiredGoods(currentId,page1);

        PageResult<RequiredGoodsVO> pageResultVO = new PageResult<>();
        pageResultVO.setTotal(pageResult.getTotal());
        pageResultVO.setRecords(pageResult.getRecords());
        return pageResultVO;
    }
}
